package com.llc.gulimall.order.interceptor;

import com.llc.common.constant.AuthServerConstant;
//import com.llc.common.vo.MemberInfoVo;
import com.llc.common.vo.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author wangwei
 * 2021/1/18 16:10
 *
 * 登录拦截器
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor{
    public static ThreadLocal<MemberResponseVo> loginUser = new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
        String uri = request.getRequestURI();
        boolean match = new AntPathMatcher().match("/order/order/status/**",uri);
        if(match){
            return true;
        }
        HttpSession session = request.getSession();
        MemberResponseVo attribute = (MemberResponseVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
        loginUser.set(attribute);
        System.out.println("order_arrribute:"+attribute);
        if(attribute != null){
            return true;
        }
        else {
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}
//    @Component
//public class LoginUserInterceptor implements HandlerInterceptor {
//
//    public static final ThreadLocal<MemberResponseVo> threadLocal = new ThreadLocal<>();
//
//    /**
//     * 目标资源访问前拦截
//     * @param request
//     * @param response
//     * @param handler
//     * @return
//     * @throws Exception
//     */
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        HttpSession session = request.getSession();
//        MemberResponseVo attribute = (MemberResponseVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
//        if (attribute == null) {
//            // 用户未登录
//            response.sendRedirect("http://auth.gulimall.com/login.html");
//            return false;
//        } else {
//            // 已登录，用threadLocal共享数据
//            threadLocal.set(attribute);
//            return true;
//        }
//    }
//}
